import { consts } from "@/module-00-codebase/pkg-00-const";
import { Bi } from "@/module-00-codebase/pkg-01-container";
import { Menu } from "@/module-00-codebase/pkg-08-entity/menu";
export class MenuManagerImpl {
  public async getMenus() {
    let menus = [] as Menu[];
    try {
      if (!!Bi.cacheManager.read("token")) {
        const user = await Bi.userManager.getUser();
        if (!!user) {
          const menu_res = await Bi.rpc.send(pullMenuParams);
          const list = (menu_res?.data?.list as Menu[]).sort((a, b) => a.name.localeCompare(b.name));
          list.forEach(menu => {
            if (!!menu.parent) {
              if (!menus.includes(menu.parent)) {
                menus.push(menu.parent);
                menu.parent.children = [];
              }
              menu.parent.children.push(menu);
            }
          });
        }
      }
    } catch (ex: unknown) {
      const e = ex instanceof Error ? ex : new Error(JSON.stringify(ex));
      Bi.loading.on(e.message, consts.error_loading_ms);
    }
    return menus;
  }
}
export const dependencies = () => ["Rpc", "UserManager", "CacheManager"];
const pullMenuParams = {
  resourceName: "innerMenu",
  action: "getlist",
  data: {
    criteria: {
      criterions: [
        { op: "eq", property: "children.id", value: null },
        { op: "ne", property: "parent.id", value: null }
      ]
    }
  }
};
